//
//  AppTools.m
//  On The Load
//
//  Created by 我爱的妞 on 15/11/5.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "AppTools.h"
#import <AFNetworking/AFNetworking.h>
#import "AppDelegate.h"

@implementation AppTools

+ (void)getAFNPOST:(NSString *)string andpara:(NSDictionary *)para andBlock:(AFNblock)block andfailBlock:(Failblock)failblock{
    
    AFNetworkReachabilityManager *Netmanger = [AFNetworkReachabilityManager sharedManager];
    
    // 1.创建AFN管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    // 2.利用AFN管理者发送请求
    NSLog(@"%@", string);
    [manager POST:string parameters:para success:^(NSURLSessionDataTask *task, id responseObject) {
        [Netmanger stopMonitoring];
        NSLog(@"请求成功---%@", responseObject);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"请求失败---%@", error);
    }];
    
}

+ (void)getAFN:(NSString *)string andpara:(NSDictionary *)para andBlock:(AFNblock)block andfailBlock:(Failblock)failblock
{
    
    AFNetworkReachabilityManager *Netmanger = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = string;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager.requestSerializer setValue:@"mxtrip/1.2.1 (iPhone; iOS 9.1; Scale/2.00) Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
    
    [manager GET:url_string parameters:para success:^(NSURLSessionDataTask *operation, id responseObject) {
        [Netmanger stopMonitoring];
        block(responseObject);
    }failure:^(NSURLSessionDataTask *operation, NSError *error) {
        failblock (error);
    }];
    
}

+ (CGFloat)heightForLabel:(NSString *)content and:(CGFloat)fontSize andWidth:(CGFloat)width{
    
    //根据内容计算高度
    //参数1.指定内容宽度来计算内容高度
    //参数2.指定计算高度的方式
    //参数3.指定内容文字大小来计算内容高度
    //参数4.苹果备用参数,设置nil
    CGSize size = CGSizeMake(width, 100000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize], NSFontAttributeName, nil];
    
    CGRect rect = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.height;
}

+ (CGFloat)widthForLabel:(NSString *)content and:(CGFloat)fontSize andHeight:(CGFloat)height{
    
    //根据内容计算宽度
    //参数1.指定内容高度来计算内容宽度
    //参数2.指定计算宽度的方式
    //参数3.指定内容文字大小来计算内容宽度
    //参数4.苹果备用参数,设置nil
    CGSize size = CGSizeMake(100000, height);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize], NSFontAttributeName, nil];
    
    CGRect rect = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return rect.size.width;
}

//获得当前时间
+ (NSString *)getCurrentTime
{
    //    NSDate *nowDate = [NSDate dateWithTimeIntervalSinceNow:0];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    //设置显示日期格式
    [formatter setDateFormat:@"yyyy / MM / dd"];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger seconds = [zone secondsFromGMTForDate:[NSDate date]];
    //获取当地时间
    NSDate *localDate = [NSDate dateWithTimeIntervalSinceNow:0];
    //将date转换成字符串
    NSString *timeStr = [formatter stringFromDate:localDate];
    
    //    return nowDate;
    return timeStr;
}

//反归档
+ (NSMutableArray *)getArrayFromFile:(NSString *)fileName{
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //获取document文件夹路径
    NSString *documentPath = [array lastObject];
    
    //NSLog(@"documentPath = %@", documentPath);
    //往本地写入数组
    NSString *arrayFilePath = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.xml", fileName]];
    
    NSMutableArray *dicArray = [NSMutableArray arrayWithContentsOfFile:arrayFilePath];
    
    return dicArray;
}

+ (void)saveArrayToFile:(NSString *)fileName andArray:(NSArray *)xmlArray{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //获取document文件夹路径
    NSString *documentPath = [array lastObject];
    
    NSLog(@"documentPath = %@", documentPath);
    NSString *name = [NSString stringWithFormat:@"%@.xml", fileName];
    NSString *fileXmlTxt = [documentPath stringByAppendingPathComponent:name];
    
    [xmlArray writeToFile:fileXmlTxt atomically:YES];
}

+ (CGFloat)heightForCell:(NSString *)content andwide:(CGFloat)wide
{
    //根据内容算内容高度
    //参数1.指定内容宽度来计算内容高度
    //参数2.指定计算高度的方式
    //参数3.指定内容文字大小来计算内容高度
    //参数4.苹果备用参数,设置为nil
    CGSize size = CGSizeMake(wide, 10000);//wide一定要和label宽度相同
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName, nil];
    
    CGRect rect = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

//+ (void)

@end
